package com.lc.stock.module.crawldata.impl;

import com.alibaba.fastjson.JSONArray;
import com.lc.stock.common.Constant;
import com.lc.stock.common.CrawDataAPI;
import com.lc.stock.module.crawldata.AbstractCrawData;
import com.lc.stock.module.stockdailyline.dao.StockDailyLineDao;
import com.lc.stock.module.stockdailyline.domain.StockDailyLine;
import com.lc.stock.module.stockdailyline.domain.StockDailyLineDetails;
import com.lc.stock.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lichen
 * @description 爬取公司日线交易数据
 * （抓取日线交易数据是为了后续研究筹码分布算法需要，由于日线交易数据过多，暂时不爬取）
 * @menu
 * @date 2023/2/7 9:12
 */
@Slf4j
//@Service
public class CrawStockDailyLineService extends AbstractCrawData {

    @Autowired
    private HttpClientUtil httpClientUtil;
    @Autowired
    private StockDailyLineDao stockDailyLineDao;

    /**
     * 爬取公司日线交易数据
     * @param stockCode
     */
    @Override
    public void doCraw(String stockCode) {
        try{
            String resp = httpClientUtil.get(CrawDataAPI.STOCK_DAILY_LINE.getUrl(), stockCode, Constant.dq);
            List<StockDailyLineDetails> stockDailyLineDetails = JSONArray.parseArray(resp, StockDailyLineDetails.class);
            StockDailyLine stockDailyLine = new StockDailyLine();
            stockDailyLine.setStockCode(stockCode);
            stockDailyLine.setStockDailyLines(stockDailyLineDetails);

            //保存、更新数据库
            StockDailyLine byStockCode = stockDailyLineDao.findByStockCode(stockCode);
            if(null != byStockCode){
                stockDailyLine.set_id(byStockCode.get_id());
            }
            stockDailyLineDao.save(stockDailyLine);
        }catch (Exception e){
            log.error("抓取公司" + stockCode + "日线交易数据失败",e);
        }
    }

    @Override
    public String getSubClassName() {
        return this.getClass().getName();
    }
}
